

<template>
  <div id="app" name="app">
    <el-container>
      <!-- 顶部导航栏 -->
      <div class="topbar">
        <div class="nav">
          <a
            ><router-link to="/"
              ><i class="el-icon-s-shop">大川商城</i></router-link
            ></a
          >
          <ul>
            <li v-if="!this.$store.getters.getUser">
              <el-button type="text" @click="login" style="font-size: 16px"
                ><i class="el-icon-user"></i>登录</el-button
              >
              <span class="sep">|</span>
              <el-button
                type="text"
                @click="register = true"
                style="font-size: 16px; margin-right: 15px"
                >注册</el-button
              >
            </li>
            <li v-else>
              <span style="color: #f0fff4; margin-right: 5px; font-size: 20px"
                >您好</span
              >


              <el-menu mode="horizontal" style="line-height: 64px ; background-color: #3d3d3d;
              border: none ;margin-top: -11px; margin-left: -5px">
                <el-submenu
                  :show-timeout="200"
                  :hide-timeout="100"
                  popper-class="my-submenu"
                  placement="bottom-start"
                >
                  <template slot="title">
                    <span style="font-size: large; margin-top: 2px; color:#b0b0b0">{{ this.$store.getters.getUser }}</span>
                  </template>
                  <el-menu-item @click="isEdit = true">修改密码</el-menu-item>
                  <el-menu-item @click="logout()">登出</el-menu-item>
                </el-submenu>
              </el-menu>
            </li>
            <li>
              <router-link to="/order"
                ><i class="el-icon-files"></i>我的订单</router-link
              >
            </li>
            <li>
              <router-link to="/collect"
                ><i class="el-icon-star-on"></i>我的收藏</router-link
              >
            </li>
            <li :class="getNum > 0 ? 'shopCart-full' : 'shopCart'">
              <router-link to="/shoppingCart">
                <i class="el-icon-shopping-cart-full"></i> 购物车
                <span class="num" style="color: #ff6700">({{ getNum }})</span>
              </router-link>
            </li>
          </ul>
        </div>
      </div>

      <div>
        <el-dialog title="修改密码" width="300px" center :visible.sync="isEdit">
          <el-form
            :model="editUser"
         
            status-icon
       
            class="demo-ruleForm"
          >
            <el-form-item prop="name">
              <span>电话</span>
              <el-input
                prefix-icon="el-icon-user-solid"
                placeholder="请输入电话"
                v-model="editUser.tel"
              ></el-input>
            </el-form-item>
            <el-form-item prop="pass">
              <span>原密码</span>
              <el-input
                prefix-icon="el-icon-view"
                type="password"
                placeholder="请输入原密码"
                v-model="editUser.oldPass"
              ></el-input>
            </el-form-item>

            <el-form-item prop="pass">
              <span>新密码</span>
              <el-input
                prefix-icon="el-icon-view"
                type="password"
                placeholder="请输入新密码"
                v-model="editUser.newPass"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button
                size="medium"
                type="primary"
                @click="isEdit = false"
                style="width: 100%"
                >取消</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button
                size="medium"
                type="primary"
                @click="confirmEdit"
                style="width: 100%"
                >修改</el-button
              >
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>
      <!-- 顶部导航栏END -->

      <!-- 顶栏容器 -->
      <el-header>
        <el-menu
          :default-active="activeIndex"
          class="el-menu-demo"
          mode="horizontal"
          active-text-color="#409eff"
          router
        >
          <el-menu-item class="logo"
            ><router-link to="/">
              <img src="./assets/imgs/logo.png" alt style="width: 105px"/> </router-link
          ></el-menu-item>

          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/goods">全部商品</el-menu-item>
        

          <div class="so">
            <el-input placeholder="请输入搜索内容" v-model="search">
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="searchClick"
                style="background-color: #ffea7f; color: #1b1f23"
              ></el-button>
            </el-input>
          </div>
        </el-menu>
      </el-header>
      <!-- 顶栏容器END -->

      <!-- 登录模块 -->
      <MyLogin></MyLogin>
      <!-- 注册模块 -->
      <MyRegister :register="register" @fromChild="isRegister"></MyRegister>

      <!-- 主要区域容器 -->
      <el-main>
        <keep-alive>
          <router-view></router-view>
        </keep-alive>
      </el-main>
      <!-- 主要区域容器END -->

      <!-- 底栏容器 -->
      <el-footer>
        <div class="footer">
          <a
            href="#"
            id="navBackToTop"
            aria-label="回到顶部"
            onclick="document.body.scrollTop = 0;
          document.documentElement.scrollTop = 0; event.preventDefault();"
          >
            <div class="BackToTop">
              <span class="TOP">回到顶部</span>
            </div>
          </a>
          <div class="ng-promise-box">
            <div class="ng-promise">
              <p class="text">
                <a class="icon1" href="javascript:"
                  ><span style="font-size: 50px">7</span>天无理由退换货</a
                >
                <a class="icon2" href="javascript:"
                  ><span style="font-size: 50px">99</span>元全场包邮</a
                >
                <a class="icon3" style="margin-right: 0" href="javascript:;">
                  <span style="font-size: 50px">100</span>%品质保证</a
                >
              </p>
            </div>
          </div>
          <div>
            <p style="color: white; font-size: 16px">
              <i class="el-icon-message"></i>
              666@gmail.com
            </p>
            <br />
            <p style="color: white; font-size: 16px">
              <i class="el-icon-phone"></i>10086
            </p>
            <br />
            <p style="color: white; font-size: 16px">
              <i class="el-icon-s-promotion" href="www.twitter.com"></i>
              @lossanien
            </p>
          </div>
          <div class="mod_help">
            <p>
              <router-link to="/about">了解我们</router-link>
              <span>|</span>
              <router-link to="/goods">商品选择</router-link>
              <span>|</span>
              <a href="https://www.neusoft.com/cn/">品牌介绍</a>
            </p>
            <p class="coty">&copy; 2002-2023 四川大学版权所有</p>
          </div>
        </div>
      </el-footer>
      <!-- 底栏容器END -->
    </el-container>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { mapGetters } from "vuex";
import userClient from "./util/request";
export default {
  beforeUpdate() {
    this.activeIndex = this.$route.path;
  },
  data() {
    return {
      activeIndex: "", // 头部导航栏选中的标签
      search: "", // 搜索条件
      register: false, // 是否显示注册组件
      visible: false, // 是否退出登录
      isEdit: false,
      editUser: {
        tel: "",
        oldPass: "",
        newPass: "",
      },
    };
  },
  created() {
    // 获取浏览器localStorage，判断用户是否已经登录
    if (localStorage.getItem("user")) {
      // 如果已经登录，设置vuex登录状态
      this.setUser(JSON.parse(localStorage.getItem("user")));
    }
    /* window.setTimeout(() => {
      this.$message({
        duration: 0,
        showClose: true,
        message: `
        <p>如果觉得这个项目还不错，</p>
        <p style="padding:10px 0">您可以给项目源代码仓库点Star支持一下，谢谢！</p>
        <p><a href="https://github.com/hai-27/vue-store" target="_blank">Github传送门</a></p>`,
        dangerouslyUseHTMLString: true,
        type: "success"
      });
    }, 1000 * 60); */
  },
  computed: {
    ...mapGetters(["getUser", "getNum"]),
  },
  watch: {
    // 获取vuex的登录状态
    getUser: function (val) {
      if (val === "") {
        // 用户没有登录

        console.log("用户未登录");
        this.setShoppingCart([]);
      } else {
        // 用户已经登录,获取该用户的购物车信息

        // this.$axios
        //   .post("/api/user/shoppingCart/getShoppingCart", {
        //     user_id: val.user_id,
        //   })
        //   .then((res) => {
        //     if (res.data.code === "001") {
        //       // 001 为成功, 更新vuex购物车状态
        //       this.setShoppingCart(res.data.shoppingCartData);
        //     } else {
        //       // 提示失败信息
        //       this.notifyError(res.data.msg);
        //     }
        //   })
        //   .catch((err) => {
        //     return Promise.reject(err);
        //   });
        console.log("设置购物车");
        userClient
          .get("/userapi/cart/list")
          .then((res) => {
            if (res.data.code === 200) {
              //200 为成功, 更新vuex购物车状态
              this.setShoppingCart(res.data.data);
            } else {
              // 提示失败信息
              this.notifyError(res.data.message);
            }
          })
          .catch((err) => {
            return Promise.reject(err);
          });
      }
    },
  },
  methods: {
    ...mapActions(["setUser", "setShowLogin", "setShoppingCart"]),
    login() {
      // 点击登录按钮, 通过更改vuex的showLogin值显示登录组件
      this.setShowLogin(true);
    },
    // 退出登录
    logout() {
      this.visible = false;
      // 清空本地登录信息
      localStorage.setItem("user", "");
      // 清空vuex登录信息
      this.setUser("");
      this.notifySucceed("成功退出登录");
    },
    // 接收注册子组件传过来的数据
    isRegister(val) {
      this.register = val;
    },
    // 点击搜索按钮
    searchClick() {
      if (this.search != "") {
        // 跳转到全部商品页面,并传递搜索条件
        this.$router.push({ path: "/goods", query: { search: this.search } });
        this.search = "";
      }
    },

    confirmEdit() {
      let getcode = "/userapi/sso/getAuthCode?telephone=" + this.editUser.tel;
      userClient.get(getcode).then((res) => {
        let auth = res.data.data;

        let upd =
          "/userapi/sso/updatePassword?authCode=" +
          auth +
          "&password=" +
          this.editUser.newPass +
          "&telephone=" +
          this.editUser.tel 
     
        this.$axios
          .post(upd)
          .then((res) => {
            // “001”代表注册成功，其他的均为失败
            if (res.data.code === 200) {
              // 隐藏注册组件
              this.isRegister = false;
              // 弹出通知框提示注册成功信息
              this.notifySucceed(res.data.message);
              this.isEdit = false
            } else {
              // 弹出通知框提示注册失败信息
              this.notifyError(res.data.message);
            }

            console.log(res.data);
          })
          .catch((err) => {
            return Promise.reject(err);
          });
      });

    },

    editProfile() {
      // 执行修改个人信息的操作

      this.isEdit = true;

      
    },
  },
};
</script>

<style>
/* 全局CSS */
* {
  padding: 0;
  margin: 0;
  border: 0;
  list-style: none;
}
#app .el-header {
  padding: 0;
}
#app .el-main {
  min-height: 300px;
  padding: 20px 0;
}
#app .el-footer {
  padding: 0;
}
a,
a:hover {
  text-decoration: none;
}
/* 全局CSS END */

/* 顶部导航栏CSS */
.topbar {
  height: 40px;
  background-color: #3d3d3d;
  margin-bottom: 0px;
}
.topbar .nav {
  width: 1225px;
  margin: 0 auto;
}
.topbar .nav a{
  height: 40px;
  color: #b0b0b0;
  font-size: 16px;
  text-align: center;
  line-height: 40px;
  margin-left: 0;
  margin-right: 15px;
}
.topbar .nav ul {
  float: right;
}
.topbar .nav li {
  float: left;
  height: 40px;
  color: #b0b0b0;
  font-size: 14px;
  text-align: center;
  line-height: 40px;
  margin-left: 20px;
}
.topbar .nav .sep {
  color: #b0b0b0;
  font-size: 12px;
  margin: 0 5px;
}
.topbar .nav li .el-button {
  color: #b0b0b0;
}
.topbar .nav .el-button:hover {
  color: #fff;
}
.topbar .nav li a {
  color: #b0b0b0;
}
.topbar .nav a:hover {
  color: #fff;
}
.topbar .nav .shopCart {
  width: 120px;
  background: #424242;
}
.topbar .nav .shopCart:hover {
  background: #fff;
}
.topbar .nav .shopCart:hover a {
  color: #ff6700;
}
.topbar .nav .shopCart-full:hover a{
  color: #ffea7f;
}
.topbar .nav .shopCart-full {
  width: 120px;
  background: #ff6700;
}
.topbar .nav .shopCart-full a {
  color: white;
}
/* 顶部导航栏CSS END */

/* 顶栏容器CSS */

.el-header .el-menu {
  max-width: 1225px;
  margin: 0 auto;
  background-color: #f0f0f0;
}
.el-header .logo {
  height: 60px;
  width: 140px;
  float: left;
  margin-right: 100px;
}
.el-header .so {
  margin-top: 10px;
  width: 300px;
  float: right;
}
/* 顶栏容器CSS END */

/* 底栏容器CSS */
.footer {
  width: 100%;
  text-align: center;
  background: #2f2f2f;
  padding-bottom: 20px;
}
.footer .ng-promise-box {
  border-bottom: 1px solid #3d3d3d;
  line-height: 120px;
}
.footer .ng-promise-box {
  margin: 0 auto;
  border-bottom: 1px solid #3d3d3d;
  line-height: 120px;
}
.footer .ng-promise-box .ng-promise p a {
  color: #fff;
  font-size: 20px;
  margin-right: 210px;
  padding-left: 44px;
  height: 40px;
  display: inline-block;
  line-height: 40px;
  text-decoration: none;
}
.footer .github {
  height: 50px;
  line-height: 50px;
  margin-top: 20px;
}

.footer .mod_help {
  text-align: center;
  color: #888888;
}
.footer .mod_help p {
  margin: 20px 0 16px 0;
}

.footer .mod_help p a {
  color: #888888;
  text-decoration: none;
}
.footer .mod_help p a:hover {
  color: #fff;
}
.footer .mod_help p span {
  padding: 0 22px;
}
.icon1{
  background: url("./assets/imgs/us-first.png") no-repeat left 0;
}

.icon2{
  background: url("./assets/imgs/us-second.png") no-repeat left 0;
}

.icon3{
  background: url("./assets/imgs/us-third.png") no-repeat left 0;
}
.BackToTop {
  margin-bottom: 20px;
  background-color: #37475A;
  display: block;
  text-align: center;
  color: #111;
  padding: 15px 0;
  line-height: 19px;
  font-size: 13px;
}


.BackToTop .TOP {
  color: #FFF;
}
hr {
  border: none;
  border-top: 1px solid #ccc;
  margin: 20px 0;
}

.el-header {
  background-color: #f0f0f0; /* set the background color */
}

/* 底栏容器CSS END */
/* 底栏容器CSS END */
</style>